package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.entity.StudentVo;
import com.hzit.service.StudentService;
import com.hzit.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService ;
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}

	//1. 根据请求参数调用不同的处理方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.1 处理post请求的中文乱码
		req.setCharacterEncoding("UTF-8");
		//1.2 设置响应的字符集编码
		resp.setContentType("text/html;charset=utf8");
		//1.3 开始得到cmd参数
		String cmd = req.getParameter("cmd");
		//1.4 如果存在此cmd参数，就调用不同方法
		if(StrUtil.isNotBlank(cmd)){
			if("list".equals(cmd)){
				list(req,resp);
			}
			if("findByPage".equals(cmd)){
				findByPage(req,resp);
			}
			if("search".equals(cmd)){
				search(req,resp);
			}
			if("add".equals(cmd)){
				add(req,resp);
			}
			if("update".equals(cmd)){
				update(req,resp);
			}
			if("delete".equals(cmd)){
				delete(req,resp);
			}
		}
	}


	//2. 学生列表
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//2.1 查询学生列表
		List<Student> studentList = studentService.findAll();
		//2.2 将学生对应输出页面
		resp.getWriter().println(JSON.toJSONString(studentList));
		//2.3 刷新流
		resp.getWriter().flush();
	}
	//3. 分页查询
	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//3.1 得到分页查询参数
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		//3.2 转换为当前页及每页大小
		int page = Integer.parseInt(pageStr);
		int pageSize = Integer.parseInt(pageSizeStr);
		//3.3 开始分页
		PageResult<Student> pageResult = studentService.findByPage(page,pageSize);
		//3.4 将学生对应输出页面
		resp.getWriter().println(JSON.toJSONString(pageResult));
		//3.5 刷新流
		resp.getWriter().flush();
	}

	//4. 分页带条件查询
	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//4.1 得到分页查询参数
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		String stud = req.getParameter("stud");

		//4.2 转换为当前页及每页大小
		int page = Integer.parseInt(pageStr);
		int pageSize = Integer.parseInt(pageSizeStr);
		//4.3 得到的查询条件对象
		StudentVo studentVo = JSON.parseObject(stud, StudentVo.class);
		//4.4 开始分页
		PageResult<Student> pageResult = studentService.search(page,pageSize,studentVo);

		//3.4 将学生对应输出页面
		resp.getWriter().println(JSON.toJSONString(pageResult));
		//3.5 刷新流
		resp.getWriter().flush();
	}
	//5. 添加学生
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//5.1 得到添加学生的数据
		String stud = req.getParameter("stud");
		//5.2 转换为 学生对象
		Student student = JSON.parseObject(stud,Student.class);
		R r = null;
		//5.3 添加学生到数据库
		try {
			studentService.add(student);
			r = R.ok("添加成功！");
		} catch (IOException e) {
			e.printStackTrace();
			r = R.error("添加失败,原因是：" + e.getMessage());
		}
		//5.4 将学生对应输出页面
		resp.getWriter().println(JSON.toJSONString(r));
		//5.5 刷新流
		resp.getWriter().flush();
	}
	//6. 修改学生
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//6.1 得到添加学生的数据
		String stud = req.getParameter("stud");
		//6.2 转换为 学生对象
		Student student = JSON.parseObject(stud,Student.class);
		R r = null;
		//6.3 添加学生到数据库
		try {
			studentService.update(student);
			r = R.ok("修改成功！");
		} catch (IOException e) {
			e.printStackTrace();
			r = R.error("修改失败,原因是：" + e.getMessage());
		}
		//6.4 将学生对应输出页面
		resp.getWriter().println(JSON.toJSONString(r));
		//6.5 刷新流
		resp.getWriter().flush();
	}

	//7. 删除学生
	private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//7.1 得到要删除学生的编号的数据
		String sid = req.getParameter("sid");
		R r = null;
		//7.2 删除学生
		try {
			studentService.delete(sid);
			r = R.ok("删除学生成功！");
		} catch (IOException e) {
			e.printStackTrace();
			r = R.error("删除学生失败，原因是：" + e.getMessage());
		}
		//7.3 将学生对应输出页面
		resp.getWriter().println(JSON.toJSONString(r));
		//7.4 刷新流
		resp.getWriter().flush();
	}
}
